﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace BtLabelTool
{
    public class CommonHelper
    {
        public static void Delay(int Millsecond)
        {
            DateTime current = DateTime.Now;
            while (current.AddMilliseconds(Millsecond) > DateTime.Now)
            {
                Thread.Sleep(100);
            }
            return;
        }
        /// <summary>  
        /// 获取时间戳  13位
        /// </summary>  
        /// <returns></returns>  
        public static long GetTimeStamp()
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalSeconds * 1000);
        }

        /// <summary>
        /// 16进制字节转字符串
        /// </summary>
        /// <param name="bt"></param>
        /// <returns></returns>
        public static string GetByteStr(byte[] bt)
        {
            string str = "";
            foreach (var item in bt)
            {
                str += item.ToString("X").PadLeft(2, '0') + " ";
            }
            return str.Trim();
        }
        /// <summary>
        /// 字节对比
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static bool EqualBytes(byte[] a, byte[] b)
        {
            if (a.Length!=b.Length)
            {
                return false;
            }
            bool result = true;
            for (int i = 0; i < a.Length; i++)
            {
                if (a[i] != b[i])
                {
                    result = false;
                    break;
                }
            }
            return result;
        }

        /// <summary>
        /// 32位MD5加密
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns>
        public static string MD5Encrypt32(string password)
        {
            string cl = password;
            string pwd = "";
            MD5 md5 = MD5.Create(); //实例化一个md5对像
                                    // 加密后是一个字节类型的数组，这里要注意编码UTF8/Unicode等的选择　
            byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
            // 通过使用循环，将字节类型的数组转换为字符串，此字符串是常规字符格式化所得
            //for (int i = 0; i < s.Length; i++)
            //{
            //    // 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母，如果使用大写（X）则格式后的字符是大写字符 
            //    pwd = pwd + s[i].ToString("x2");
            //}
            return BitConverter.ToString(s).Replace("-", "");
        }
    }
}
